package com.hrt.datasender.datasent.onenet;

import java.io.IOException;
import java.io.OutputStream;

import com.btcode.common.MyLog;
import com.btcode.log.ILog;
import com.hrt.datasender.mq.model.PacketModel;

/**
 *
 * <pre>
 * 注释
 * </pre>
 * @author FAN
 * @date 2018年10月24日下午4:31:25
 */
public class OneNetManager {

	private ILog log;
	private static OneNetManager _instance;
	private OutputStream os;



	public void setOs(OutputStream os) {
		this.os = os;
	}

    public static OneNetManager getInstance() {
        if (_instance == null) {
            _instance = new OneNetManager();
        }
        return _instance;
    }


    public synchronized void sendMsg(PacketModel model) {

    	log = MyLog.getInstance().getLogger(OneNetManager.class.getName());

    	String msg = "AT+MIPLNOTIFY=0,3336,0,5516,1,\""+model.getMsg()+"\",1";


    	msg = msg.replaceAll("\r|\n", "");

    	msg = msg.replaceAll(";",".");

		msg = msg+"\r";

    	byte [] bymsg = msg.getBytes();

    	try {
        	os.write(bymsg);

        	log.info("发送成功:"+msg);

        	System.out.println("发送成功:"+msg);

           Thread.sleep(2000);
        } catch (Exception e) {
            log.error("发送数据出错" + model, e);
        }
    }

    public synchronized void sendRefreshRegisterMsg(PacketModel model) {

    	log = MyLog.getInstance().getLogger(OneNetManager.class.getName());

    	String msg = model.getMsg()+"\r";

    	byte [] bymsg = msg.getBytes();

    	try {
			os.write(bymsg);

			log.info("发送成功:"+msg);

			System.out.println("发送成功:"+msg);

			Thread.sleep(2000);
		} catch (IOException | InterruptedException e) {
			e.printStackTrace();
			log.error("发送数据出错" + model, e);
		}
    }


}
